import React, {Component} from 'react';
import {inject, observer} from 'mobx-react';
import {Modal} from 'antd';
import * as mobx from 'mobx';
import {stopEvent, getInputKeys} from '../../util/TreeUtil';
import SearchFavourite from '../../util/SearchFavourite';

@inject('favouriteDirStore', 'sysFavouriteStore', 'myFavouriteStore')
@observer
export default class FavDirBar extends Component {
    /**
     * 新建目录事件
     * @param evt
     */
    addTreeNode = (evt) => {
        const {favouriteDirStore} = this.props;
        const {addBtnDisable = false} = favouriteDirStore;
        
        if (!addBtnDisable) {
            favouriteDirStore.curTree.tree.addRootNode();

            //点击了添加后，将按钮置为不可用，当新建的目录保存后，再置为可用
            favouriteDirStore.controlBtnDisable(true);
        }

        stopEvent(evt);
    }

    /**
     * 全部目录，鼠标移入、移出、点击
     */
    handleRootNodeOver = (evt) => {
        const {favouriteDirStore} = this.props;
        favouriteDirStore.setRootOver(true);
    }

    handleRootNodeOut = (evt) => {
        const {favouriteDirStore} = this.props;
        favouriteDirStore.setRootOver(false);
    }

    handleRootNodeClick = (evt) => {
        const {favouriteDirStore, sysFavouriteStore} = this.props;
        favouriteDirStore.setRootClick(true);

        /**
         * 选中树根节点后，刷新收藏的列表
         */
        const isinit = true;
        const isreload = false;
        SearchFavourite.setConditions({dirId: '0'});
        const searchFavourite = new SearchFavourite(sysFavouriteStore);
        searchFavourite.load({isinit, isreload});
    }


    render() {
        const {favouriteDirStore, myFavouriteStore} = this.props;
        const {isover, selectedKey} = favouriteDirStore;
        const {allDirName, addDirTitle} = myFavouriteStore;
        let cls = 'fav-left-bar';
        if (isover) {
            cls += ' fav-left-bar-over';
        }
        if (selectedKey == '0') {
            cls += ' fav-left-bar-click';
        }

        return (
            <div
                className={cls}
                onClick={this.handleRootNodeClick}
                // onMouseOver={this.handleRootNodeOver}
                // onMouseOut={this.handleRootNodeOut}
                ecId={`${this && this.props && this.props.ecId || ''}`}
            >
                <div className="fav-left-bar-title fav-bar-common">
                    <span className="fav-left-bar-title-icon"/>
                    <span>{allDirName}</span>
                </div>
                <div
                    className="fav-left-bar-btn fav-bar-common"
                    title={addDirTitle}
                    onClick={this.addTreeNode}
                />
            </div>
        );
    }
}
